package gui2;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JSplitPane;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JMenuBar;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.SpinnerDateModel;
import javax.swing.border.TitledBorder;

import schedule.Appointment;
import schedule.AppointmentList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MainWindow extends JFrame {
	
	AppointmentList list;

	private JPanel contentPane;
	private JLabel lblTitle;
	private JTextField tfTitle;
	private JLabel lblStart;
//	private JTextField tfStart;
	private JLabel lblEnd;
//	private JTextField tfEnd;
	private JPanel panel_1;
	private JPanel panel_2;
	private JButton btnEnter;
	private JButton btnSearch;
	private JTextArea taDescription;
//	private Appointment_panel appointment_panel;
	private AppointmentPanel appointmentPanel;
	
	private JFrame search;
	private JPanel panel_3;
	private JButton btnClearList;
	
	private SpinnerDateModel startDate = new SpinnerDateModel();
	private JSpinner spStart;
	private SpinnerDateModel endDate = new SpinnerDateModel();
	private JSpinner spEnd;
	
	/**
	 * Launch the application.
	 */
//	public static void main(String[] args) {
//		EventQueue.invokeLater(new Runnable() {
//			public void run() {
//				try {
//					MainWindow frame = new MainWindow();
//					frame.setVisible(true);
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//		});
//	}

	/**
	 * Create the frame.
	 */
	public MainWindow(AppointmentList l) {
		this.list = l;
		
		setTitle("Appointments");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 564, 366);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JSplitPane splitPane = new JSplitPane();
		contentPane.add(splitPane, BorderLayout.CENTER);
		
		JPanel panel = new JPanel();
		splitPane.setLeftComponent(panel);
		panel.setLayout(new BorderLayout(0, 0));
		
		panel_1 = new JPanel();
		panel.add(panel_1, BorderLayout.NORTH);
		panel_1.setLayout(new GridLayout(3, 2, 0, 0));
		
		lblTitle = new JLabel("Title: ");
		panel_1.add(lblTitle);
		
		tfTitle = new JTextField();
		panel_1.add(tfTitle);
		tfTitle.setColumns(10);
		
		lblStart = new JLabel("Start: ");
		panel_1.add(lblStart);
		
		spStart = new JSpinner(startDate);
		spStart.setEditor(new JSpinner.DateEditor(spStart, "dd.MM.yyyy HH:mm"));
		panel_1.add(spStart);
//		tfStart = new JTextField();
//		panel_1.add(tfStart);
//		tfStart.setColumns(10);
		
		lblEnd = new JLabel("End: ");
		panel_1.add(lblEnd);
		
//		tfEnd = new JTextField();
//		panel_1.add(tfEnd);
//		tfEnd.setColumns(10);
		
		spEnd = new JSpinner(endDate);
		spEnd.setEditor(new JSpinner.DateEditor(spEnd, "dd.MM.yyyy HH:mm"));
		panel_1.add(spEnd);
		
		panel_2 = new JPanel();
		panel.add(panel_2, BorderLayout.SOUTH);
		panel_2.setLayout(new GridLayout(0, 2, 0, 0));
		
		btnEnter = new JButton("Enter");
		btnEnter.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_btnEnter_actionPerformed(arg0);
			}
		});
		panel_2.add(btnEnter);
		
		btnSearch = new JButton("Search");
		btnSearch.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				do_btnSearch_actionPerformed(arg0);
			}
		});
		panel_2.add(btnSearch);
		
		taDescription = new JTextArea();
		taDescription.setBorder(new TitledBorder(null, "Description: ", TitledBorder.LEADING, TitledBorder.TOP, null, null));
		panel.add(taDescription, BorderLayout.CENTER);
		
		panel_3 = new JPanel();
		splitPane.setRightComponent(panel_3);
		panel_3.setLayout(new BorderLayout(0, 0));
		
//		appointment_panel = new Appointment_panel(list);
//		panel_3.add(appointment_panel);
		
		appointmentPanel = new AppointmentPanel(list);
		panel_3.add(appointmentPanel);
		
//		btnClearList = new JButton("Clear List");
//		btnClearList.addActionListener(new ActionListener() {
//			public void actionPerformed(ActionEvent arg0) {
//				do_btnClearList_actionPerformed(arg0);
//			}
//		});
//		panel_3.add(btnClearList, BorderLayout.SOUTH);
	}

	protected void do_btnSearch_actionPerformed(ActionEvent arg0) {
		search = new Search(list);
	}
//	protected void do_btnClearList_actionPerformed(ActionEvent arg0) {
//		list.clearList();
//		appointment_panel.writeAppointments();
//	}
	protected void do_btnEnter_actionPerformed(ActionEvent arg0) {
//		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd hhmm");
		try{
			if(!tfTitle.getText().equals("")){
//				Date convertedDate = (Date) sdf.parse(tfStart.getText());
//				Date convertedEndDate = (Date) sdf.parse(tfEnd.getText());
//				boolean worked = list.add(new Appointment(tfTitle.getText(),convertedDate, convertedEndDate, taDescription.getText()));
				boolean worked = list.add(new Appointment(tfTitle.getText(), startDate.getDate(), endDate.getDate(), taDescription.getText()));
				list.bubbleSort();
				appointmentPanel.refresh();
				if(!worked){
					throw new Exception("please check for overlaps with existing appointments");
				}
			} else{
				throw new Exception("Title empty");
			}
		}catch(ParseException pe){
//			System.err.println("Exception: wrong date format (YYYYMMDD)");
			JOptionPane.showMessageDialog(this, pe.getMessage());
		}catch(Exception ex){
//			System.err.println(ex.getMessage());
			JOptionPane.showMessageDialog(this, ex.getMessage());
		}
	}
	
	public AppointmentPanel getAppointmentPanel(){
		return appointmentPanel;
	}
	
	public AppointmentList getList(){
		return list;
	}
}
